package month1;
import java.util.*;
public class PalindromePartitioning131_06 {
    public static void main(String[] args) {
        System.out.println(partition("aab"));
    }

    /**
     * 简单回溯吧
     * @param s
     * @return
     */
    static List<List<String>> partition(String s) {
        List<List<String>> ret = new ArrayList<>();
        traceback(s, ret, new ArrayList<>(), 0);
        return ret;
    }
    static void traceback(String s, List<List<String>> ret, List<String> tmp, int i) {
        if (i == s.length()) {
            ret.add(new ArrayList(tmp));
            return;
        }
        for(int j = i;j < s.length(); ++j) {
            String str = s.substring(i, j + 1);
            if (isPalindrome(str)) {
                tmp.add(str);
                traceback(s, ret, tmp, j + 1);
                tmp.remove(tmp.size() - 1);
            }
        }
    }
    static boolean isPalindrome(String word) {
        int l = 0, r = word.length() - 1;
        while(l < r) {
            if (word.charAt(l++) != word.charAt(r--)) {
                return false;
            }
        }
        return true;
    }
}
